﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MyVoteMVC.Domain;
using MyVoteMVC.BackstageManagement.Controllers;

namespace MyVoteMVC.BackstageManagement.Views
{
    public interface IMessageManagementView
    {
        void GetActivityListComplete(IList<VT_Activities> list);
        void GetMessageListComplete(IList<VT_Messages> list);
        void DeleteMessageComplete(string message);
        void FindMessageComplete(IList<VT_Messages> list);
    }

    public partial class MessageManagement : Form,IMessageManagementView
    {
        private IMessageController controller;
        public MessageManagement()
        {
            InitializeComponent();
            controller = new MessageController(this);
        }


        public void GetActivityListComplete(IList<VT_Activities> list)
        {
            lstActivity.Items.Clear();
            foreach (VT_Activities vs in list)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.Activity_ID;
                lvItem.SubItems.Add(vs.Activity_Title);
                lstActivity.Items.Add(lvItem);
            }
        }

        public void GetMessageListComplete(IList<VT_Messages> list)
        {
            lstMessage.Items.Clear();
            foreach (VT_Messages vs in list)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.Message_ID;
                lvItem.SubItems.Add(vs.Activity_ID);
                lvItem.SubItems.Add(vs.Message_Title);
                lvItem.SubItems.Add(vs.Message_Content);
                lstMessage.Items.Add(lvItem);
            }
        }

        public void DeleteMessageComplete(string message)
        {
            MessageBox.Show(message);
            clear();
            lstActivity_Click(null, null);
        }

        public void FindMessageComplete(IList<VT_Messages> list)
        {
            lstMessage.Items.Clear();
            foreach (VT_Messages vs in list)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.Message_ID;
                lvItem.SubItems.Add(vs.Activity_ID);
                lvItem.SubItems.Add(vs.Message_Title);
                lvItem.SubItems.Add(vs.Message_Content);
                lstMessage.Items.Add(lvItem);
            }
        }

        private void MessageManagement_Load(object sender, EventArgs e)
        {
            controller.GetActivityList();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            controller.FindMessage(txtFind.Text.Trim());
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            controller.DeleteMessage(lblActivityNo.Text.Trim(), lblMessageNo.Text.Trim()); 
        }

        private void lstActivity_Click(object sender, EventArgs e)
        {
            if (this.lstMessage.SelectedItems != null)  //判断ListView控件是否有项目选中
            {
                controller.GetMessageList(this.lstActivity.SelectedItems[0].SubItems[0].Text);
            }
        }

        private void lstMessage_Click(object sender, EventArgs e)
        {
            if (this.lstMessage.SelectedItems != null)  //判断ListView控件是否有项目选中
            {
                lblMessageNo.Text = this.lstMessage.SelectedItems[0].SubItems[0].Text;
                lblActivityNo.Text = this.lstMessage.SelectedItems[0].SubItems[1].Text;
                lblTitle.Text = this.lstMessage.SelectedItems[0].SubItems[2].Text;
                txtMessageContent.Text = this.lstMessage.SelectedItems[0].SubItems[3].Text; 
            }                    
        }

        public void clear()
        {
            lblMessageNo.Text = "";
            lblActivityNo.Text = "";
            lblTitle.Text = "";
            txtMessageContent.Text = "";
        }
    }
}
